package gui.reports.productstats;

import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;

import core.ProductManager;
import core.reports.HTMLBuilder;
import core.reports.IReportBuilder;
import core.reports.IReportVisitor;
import core.reports.PDFBuilder;
import core.reports.ProductStatsVisitor;
import gui.common.*;

/**
 * Controller class for the product statistics report view.
 */
public class ProductStatsReportController extends Controller implements
		IProductStatsReportController {

	private int months;
	/**
	 * Constructor.
	 * 
	 * @param view Reference to the item statistics report view
	 */
	public ProductStatsReportController(IView view) {
		super(view);
		
		construct();
	}

	//
	// Controller overrides
	//
	
	/**
	 * Returns a reference to the view for this controller.
	 * 
	 * {@pre None}
	 * 
	 * {@post Returns a reference to the view for this controller.}
	 */
	@Override
	protected IProductStatsReportView getView() {
		return (IProductStatsReportView)super.getView();
	}

	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	@Override
	protected void enableComponents() {
		getView().enableFormat(true);
		getView().enableMonths(true);
		getView().enableOK(true);
	}

	/**
	 * Loads data into the controller's view.
	 * 
	 *  {@pre None}
	 *  
	 *  {@post The controller has loaded data into its view}
	 */
	@Override
	protected void loadValues() {
		getView().setMonths("3");
		months = 3;
	}

	//
	// IProductStatsReportController overrides
	//

	/**
	 * This method is called when any of the fields in the
	 * product statistics report view is changed by the user.
	 */
	@Override
	public void valuesChanged() {
		try
		{
			months = Integer.parseInt(getView().getMonths());
			if (months < 1 || months > 100)
				getView().enableOK(false);
			else
				getView().enableOK(true);
		}
		catch (NumberFormatException e)
		{
			getView().enableOK(false);
		}
	}
	
	/**
	 * This method is called when the user clicks the "OK"
	 * button in the product statistics report view.
	 */
	@Override
	public void display() {
		IReportBuilder builder;
		
		//get the output type, construct the proper builder
		FileFormat format = getView().getFormat();
		String title = "Product Report (" + months + " Months)";
		try {
			if (format == FileFormat.HTML) {
				builder = new HTMLBuilder(generateFilename(".html"), title);
			}
			else {
				builder = new PDFBuilder(generateFilename(".pdf"), title);
			}
		}
		catch (IOException e) {
			getView().displayErrorMessage("Cannot open a file to write to!");
			return;
		}
		
		//construct the visitor
		IReportVisitor visitor;
		try {
			visitor = new ProductStatsVisitor(builder, months);
			ProductManager.instance().accept(visitor);
			builder.print();
		} catch (IOException e) {
			getView().displayErrorMessage("Unable to print report");
		}
	}

	private String generateFilename(String extension) {
		StringBuilder dateBuilder = new StringBuilder();
		dateBuilder.append("ProductStats");
		dateBuilder.append(Calendar.getInstance().get(GregorianCalendar.MONTH));
		dateBuilder.append(Calendar.getInstance().get(GregorianCalendar.DAY_OF_MONTH));
		dateBuilder.append(Calendar.getInstance().get(GregorianCalendar.YEAR));
		dateBuilder.append(Calendar.getInstance().get(GregorianCalendar.HOUR));
		dateBuilder.append(Calendar.getInstance().get(GregorianCalendar.MINUTE));
		dateBuilder.append(Calendar.getInstance().get(GregorianCalendar.SECOND));
		dateBuilder.append(extension);
		return dateBuilder.toString();
	}
}

